package com.example.administrator.appstpremvpdemo.binder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @Author Administrator
 * @Date 2017/12/26
 * @description
 * @function ViewBinder抽象基类
 */

public abstract class BaseViewBinder extends RecyclerView.ViewHolder {

    View mRootView;
    Context mContext;
    int mItemViewPosition = -1;
    Object mItemData;
    private boolean mViewCreated = false;

    public BaseViewBinder(View itemView) {
        super(itemView);
        mRootView = itemView;
        mContext = itemView.getContext();
    }

    public BaseViewBinder(ViewGroup parent, int layoutId) {
        this(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
    }

    public void bind(Object object) {
        mItemData = object;

        if (!mViewCreated || mRootView == null) {
            mViewCreated = true;
            onViewInflate(mRootView);
        }
        onbind(object);
    }

    public void setItemPosition(int position) {
        mItemViewPosition = position;
    }

    public int getItemPostion() {
        return mItemViewPosition;
    }

    public void setOnViewClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        if (listener == null) {
            return;
        }
        mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnItemClick(BaseViewBinder.this, mRootView);
                }
            }
        });
    }

    /**
     * 当布局中控件Inflate完成
     * @param rootView
     */
    public abstract void onViewInflate(View rootView);

    /**
     * 当数据绑定完成
     * @param object
     */
    public abstract void onbind(Object object);

    public OnItemClickListener mOnItemClickListener;

    /**
     * 给adapter添加条目点击事件
     */
    public interface OnItemClickListener {
        void OnItemClick(BaseViewBinder binder, View view);
    }
}
